package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.NotifyDao;
import com.tpyule.tpclass.entity.bo.UserAnnounceBo;
import com.tpyule.tpclass.entity.dto.AnnounceQueryDto;
import com.tpyule.tpclass.entity.pojo.Notify;
import com.tpyule.tpclass.entity.pojo.User;
import com.tpyule.tpclass.entity.pojo.UserNotify;
import com.tpyule.tpclass.service.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/22 13:52
 */
@Service
public class NotifyServiceImpl implements NotifyService {
    @Autowired
    NotifyDao notifyDao;

    @Override
    public int createRemind(Notify notify) {
        return notifyDao.addNotify(notify);
    }

    @Override
    public int createAnnounce(Notify notify) {
        return notifyDao.addNotify(notify);
    }

    @Override
    public List<Notify> getListAnnounce(AnnounceQueryDto announceQueryDto) {
        announceQueryDto.setStart((announceQueryDto.getPage() - 1) * announceQueryDto.getPageSize());
        return notifyDao.getUserListAnnounce(announceQueryDto);
    }

    @Override
    public int getSizeAnnounce() {
        return notifyDao.getSizeAnnounce();
    }

    @Transactional
    @Override
    public void pullAnnounce(User user) throws Exception {
        Notify notify = notifyDao.getLastAnnounce(user);
        List<Notify> notifies = notifyDao.getNoReadAnnounce(notify);
        for (Notify notify1 : notifies) {
            UserNotify userNotify = new UserNotify();
            userNotify.setUserNotifyId(UUID.randomUUID().toString().toUpperCase());
            userNotify.setNotifyId(notify1.getNotifyId());
            userNotify.setUserId(user.getUserId());
            notifyDao.addUserNotify(userNotify);
        }
    }

    @Override
    public int addUserNotify(UserNotify userNotify) {
        return notifyDao.addUserNotify(userNotify);
    }

    @Override
    public List<UserAnnounceBo> getListAnnounce(String userId) {
        User user = new User();
        user.setUserId(userId);
        return notifyDao.getUserListAnnounce(user);
    }

    @Override
    public int getNoReadCount(String userId) {
        return notifyDao.getNoReadCount(userId);
    }

    @Override
    public int updateNoRead(UserNotify userNotify) {
        return notifyDao.updateNoRead(userNotify);
    }

    @Override
    public int updateAllNoRead(String userId) {
        return notifyDao.updateAllNoRead(userId);
    }

}
